package com.fy.fyspace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fy.fyspace.common.result.R;
import com.fy.fyspace.mapper.TestUserMapper;
import com.fy.fyspace.model.entity.TestUser;
import com.fy.fyspace.service.TestUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.fy.fyspace.common.result.R.ok;

@Service
public class TestUserServiceImpl implements TestUserService {
    @Resource
    private TestUserMapper testUserMapper;

    @Override
    public R queryByName(String name, Integer page, Integer pageSize) {
        Page<TestUser> p = new Page<>(page, pageSize);
        LambdaQueryWrapper<TestUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(TestUser::getName, name);
        Page<TestUser> result = testUserMapper.selectPage(p, wrapper);
        return ok(result);
    }

    @Override
    public R queryByAgeRange(Integer minAge, Integer maxAge, Integer page, Integer pageSize) {
        Page<TestUser> p = new Page<>(page, pageSize);
        LambdaQueryWrapper<TestUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(TestUser::getAge, minAge, maxAge);
        Page<TestUser> result = testUserMapper.selectPage(p, wrapper);
        return ok(result);
    }

    @Override
    public R queryBySexAndHobby(String sex, String hobby, Integer page, Integer pageSize) {
        Page<TestUser> p = new Page<>(page, pageSize);
        LambdaQueryWrapper<TestUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TestUser::getSex, sex).like(TestUser::getDescription, hobby);
        Page<TestUser> result = testUserMapper.selectPage(p, wrapper);
        return ok(result);
    }

    @Override
    public R queryStatistics() {
        // 统计每个年龄段的用户数量
        return ok(testUserMapper.statisticsByAge());
    }
} 